package com.pointlion.sys.mvc.admin.evaluate.paperbase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.pointlion.sys.interceptor.MainPageTitleInterceptor;
import com.pointlion.sys.mvc.common.base.BaseController;
import com.pointlion.sys.mvc.common.dto.ZtreeNode;
import com.pointlion.sys.mvc.common.model.SysMenu;
import com.pointlion.sys.mvc.common.utils.StringUtil;
import com.pointlion.sys.mvc.common.model.PaperBase;
import com.pointlion.sys.mvc.common.model.SysUser;
import com.pointlion.sys.mvc.common.model.SysOrg;
import com.pointlion.sys.mvc.common.utils.UuidUtil;
import com.pointlion.sys.plugin.shiro.ShiroKit;



@Before(MainPageTitleInterceptor.class)
public class PaperBaseController extends BaseController {
	public static final PaperBaseService service = PaperBaseService.me;
	/***
	 *
	 */
	public void getListPage(){
		setAttr("menu", PaperBase.dao.getAllPageBase());
    	render("list.html");
    }
	/***
     *
     **/
    public void listData(){
    	String curr = getPara("pageNumber");
    	String pageSize = getPara("pageSize");
    	Page<Record> page = service.getPage(Integer.valueOf(curr),Integer.valueOf(pageSize));
    	renderPage(page.getList(),"",page.getTotalRow());
    }
    /***
     *
     */
    public void save(){
    	PaperBase p = getModel(PaperBase.class);
		if(StrKit.notBlank(p.getId())){
			if(StrKit.isBlank(p.getIfstart())){//不显示
				p.setIfstart("0");
			}
			p.update();
		}else{
			p.setId(UuidUtil.getUUID());
			if(StrKit.isBlank(p.getIfstart())){//不显示
				p.setIfstart("0");
			}
			p.save();
		}
    	renderSuccess();
    }
    /***
     *
     */
    public void getEditPage(){
		//添加和修改
		String id = getPara("id");
		if(StrKit.notBlank(id)){
			PaperBase p = PaperBase.dao.getById(id);
			PaperBase parent = PaperBase.dao.getById(p.getParentId());
			setAttr("m", p);
			setAttr("p", parent);
		}
		//添加子模块
		String parentid = getPara("parentid");
		if(StrKit.notBlank(parentid)){
			PaperBase parent = PaperBase.dao.getById(parentid);
			setAttr("p", parent);
		}
    	render("edit.html");
    }
    public void changeIfStart(){
		String id = getPara("id","");
		String ifstart = getPara("ifstart","");
		PaperBase p = PaperBase.dao.getById(id);
		if(p!=null){
			p.setIfstart(ifstart);
			p.update();
			renderSuccess();
		}else{
			renderError();
		}
	}
    /***
     *
     * @throws Exception
     */
    public void delete() throws Exception{
		String ids = getPara("ids");
		String idarr[] = ids.split(",");
		for(String id : idarr){
			//先删除对应子项
			Db.update("delete from paper_base where parent_id = '"+id+"'");
			//再删除本身
			PaperBase p = PaperBase.dao.getById(id);
			if(p!=null){
				p.delete();//删除
			}
		}
    	renderSuccess("删除成功!");
    }

    public void getAllTree(){
		List<PaperBase> menuList = PaperBase.dao.getAllMenu();
		List<ZtreeNode> nodelist = PaperBase.dao.toZTreeNode(menuList,true);//数据库中的菜单
		List<ZtreeNode> rootList = new ArrayList<ZtreeNode>();//页面展示的,带根节点
		//声明根节点
		ZtreeNode root = new ZtreeNode();
		root.setId("#root");
		root.setName("根目录");
		root.setChildren(nodelist);
		root.setOpen(true);
		rootList.add(root);
		renderJson(rootList);
	}

    public void getSelectOneItmePage(){
		render("selectOneItem.html");
	}
	/**************************************************************************/
	private String pageTitle = "指标管理";//模块页面标题
	private String breadHomeMethod = "getListPage";//面包屑首页方法

	public Map<String,String> getPageTitleBread() {
		Map<String,String> pageTitleBread = new HashMap<String,String>();
		pageTitleBread.put("pageTitle", pageTitle);
		pageTitleBread.put("breadHomeMethod", breadHomeMethod);
		return pageTitleBread;
	}
}